package com.itheima.bigevent.configure;

import com.itheima.bigevent.interceptors.LoginInterceptor;
import com.itheima.bigevent.json.JacksonObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * ClassName: WebConfigure
 * Package: com.itheima.bigevent.configure
 * Description:web配置类
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-08-19-18:40
 * @Version: v1.0
 */
@Configuration
public class WebConfigure implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns("/user/register", "/user/login");
    }

    /**
     * 配置消息转换器
     * @param converters the list of configured converters to be extended
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters){
        // 新建一个消息转换器
        // 用于将 JSON 数据转换为 Java 对象，或将 Java 对象转换为 JSON 数据。
        MappingJackson2HttpMessageConverter convert = new MappingJackson2HttpMessageConverter();

        // 添加ObejctMapper(我自己定义的)
        convert.setObjectMapper(new JacksonObjectMapper());
        // 将新建的消息转换器添加到消息转换器列表中, 0代表优先级最高
        converters.add(0, convert);
    }


}
